package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import android.content.Intent;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Security;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.AdminMainMenuView;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.LockedAccountView;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.LoginView;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.UserMainMenuView;

/**
 * Presenter for the login class
 * @author Magic 7
 *
 */
public class LoginPresenter {
	private LoginView myView;
	private final String PACKAGE_NAME;
	DatabaseHelper helper;

	/**
	 * Constructor for this class
	 * @param view the view of this screen
	 */
	public LoginPresenter(LoginView view) {
		myView = view;
		PACKAGE_NAME = myView.getPackageName();
		helper = new DatabaseHelper(myView);
	}

	/**
	 * Checks to see if the account connected to the email entered is locked.
	 * If it isn't locked, the app will then attempt to login.
	 * @param email the email that was entered
	 * @param password the password that was entered
	 */
	public void login(String email, String password) {
		// pull the strings from the text fields
		try {
			// Check to see if the Account is Locked
			System.out.println("Getting Account");
			Account isThisLocked = helper.getAccount(email);
			System.out.println("Got account");
			if (isThisLocked.isLocked()) {
				Intent intent = new Intent(myView, LockedAccountView.class);
				intent.setPackage(PACKAGE_NAME);
				// intent.putExtra(myView.getPackageName(), true);
				myView.startActivity(intent);
			}

			else {
				attemptLogin(email, password);
			}
		}
		// if Strings are null (oh the humanity!), then prompt for another input
		catch (Exception fail) {
			fail.getStackTrace();
			myView.setFailedEntryText("Silly Goose, you entered nothing!");
		}

	}

	/**
	 * This will check and see if the email and password check out.
	 * If they check out, the app will log the user in
	 * @param email the email that was entered
	 * @param password the password that was entered
	 */
	public void attemptLogin(String email, String password) {
		Security s = new Security(myView);
		if (email.isEmpty()) {
			myView.setFailedEntryText("Please enter an email address");
		} else {
			Account canILogin = helper.getAccount(email);
			if (s.checkLogin(email, password)) {
				if (canILogin.isAdmin()) {// is an admin, show admin main menu
					Intent adminIntent = new Intent(myView,
							AdminMainMenuView.class);
					adminIntent.setPackage(myView.getPackageName());
					canILogin.setLoginAttempts(1);
					DataManager.setCurrentAccount(canILogin);
					myView.startActivity(adminIntent);
				}
				// else is a user, show regular main menu
				else {
					Intent userIntent = new Intent(myView,
							UserMainMenuView.class);
					// userIntent.putExtra(myView.getPackageName(), true);
					userIntent.setPackage(myView.getPackageName());
					canILogin.setLoginAttempts(1);
					DataManager.setCurrentAccount(canILogin);
					myView.startActivity(userIntent);
				}
			}

			// if it fails, check the status of the account, and display the
			// fail message
			else {
				if (canILogin.isLocked()) {
					Intent intent = new Intent(myView, LockedAccountView.class);
					intent.setPackage(myView.getPackageName());
					myView.startActivity(intent);
				} else {
					myView.setFailedEntryText(failMessage(canILogin
							.getLoginAttempts()));

				}
			}
		}
	}

	/**
	 * Sets the fail message to include the correct number of times a user has failed to login
	 * @param times the number of times that the user has failed
	 * @return a message that tells the user they have failed to login
	 */
	private String failMessage(int times) {
		String message = "Login failed " + times;
		if (times == 1) {
			message += " time. ";
		} else {
			message += " times.";
		}
		message += " Try again please.";

		return message;
	}

	/**
	 * Takes the user to a page where they can create an account
	 * @param intent the intent that will take them there
	 */
	public void createAccount(Intent intent) {
		intent.setPackage(myView.getPackageName());
		myView.startActivity(intent);
	}

	/**
	 * Takes the user to a page where they can retrieve their password
	 * @param intent the intent that will take them there
	 */
	public void forgotAccount(Intent intent) {
		intent.setPackage(myView.getPackageName());
		myView.startActivity(intent);
	}
}
